package com.cuitrip.business.setting;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.cuitrip.business.setting.model.SubmitSuccessModel;
import com.cuitrip.business.setting.ui.SuccessViewHolder;
import com.cuitrip.service.R;
import com.lab.app.BaseFragment;
import com.lab.utils.DisplayUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created on 7/28.
 */
public class SubmitSuccessActivity extends Activity {

    public static final String KEY_MODEL = "key_submitModel";
    public static final String KEY_DATA_BUNDLE="bundle";

    @Bind(R.id.out_frame)
    LinearLayout mOutFrame;
    private SuccessViewHolder mViewHolder;
    private SubmitSuccessModel mModel;
    private Bundle bundle;

    public static void launchSuccessPage(Context context, SubmitSuccessModel submitSuccessModel) {
        Intent intent = new Intent(context, SubmitSuccessActivity.class);
        intent.putExtra(KEY_MODEL, submitSuccessModel);
        context.startActivity(intent);
    }

    public static void launchSuccessPage(BaseFragment fragment, SubmitSuccessModel submitSuccessModel,int requestCode){
        Intent intent = new Intent(fragment.getHostActivity(), SubmitSuccessActivity.class);
        intent.putExtra(KEY_MODEL, submitSuccessModel);
        fragment.startActivityForResult(intent,requestCode);
    }

    public static void launchSuccessPage(BaseFragment fragment, SubmitSuccessModel submitSuccessModel,int requestCode,Bundle bundle){
        Intent intent = new Intent(fragment.getHostActivity(), SubmitSuccessActivity.class);
        intent.putExtra(KEY_MODEL, submitSuccessModel);
        intent.putExtra(KEY_DATA_BUNDLE,bundle);
        fragment.startActivityForResult(intent,requestCode);

    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);

        Intent intent = getIntent();
        if (intent.hasExtra(KEY_MODEL)) {
            mModel = (SubmitSuccessModel) intent.getSerializableExtra(KEY_MODEL);
        } else {
            finish();
        }
        if (intent.hasExtra(KEY_DATA_BUNDLE)){
            bundle = intent.getBundleExtra(KEY_DATA_BUNDLE);
        }
        buildViewHolder();
        setContentView(mViewHolder.withLayoutRes());
        ButterKnife.bind(this);
        adjustViewWidth();
        initView();
        initData();
    }

    private void buildViewHolder() {
        switch (mModel.getType()) {
            case CREATE_TRIP:
                mViewHolder = new SuccessViewHolder();
                break;
            case FEEDBACK_SUCCESS:
                mViewHolder = new SuccessViewHolder();
                break;
            case CANCEL_ORDER:
                mViewHolder = new SuccessViewHolder();
                break;
            case VERIFIED_SUCCESS:
                mViewHolder = new SuccessViewHolder();
                break;

            case COMPLETE_INSIDER_INFO:
                mViewHolder=new SuccessViewHolder();
                mViewHolder.setDataExtras(bundle);
                break;


        }
    }


    private void initView() {
        mViewHolder.build(this);
    }

    private void initData() {
        mViewHolder.render(mModel);
    }

    private void adjustViewWidth() {
        ViewTreeObserver vto = mOutFrame.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                    mOutFrame.getViewTreeObserver()
                            .removeGlobalOnLayoutListener(this);
                } else {
                    mOutFrame.getViewTreeObserver()
                            .removeOnGlobalLayoutListener(this);
                }
                int displayWidth = DisplayUtil.getDisplayWidth();
                ViewGroup.LayoutParams layoutParams = mOutFrame.getLayoutParams();
                layoutParams.width =
                        displayWidth - DisplayUtil.dip2px(24) * 2;
                mOutFrame.setLayoutParams(layoutParams);
            }
        });
    }

    @OnClick(R.id.return_action)
    public void returnTripList() {
        mViewHolder.doAction(mModel);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 如果接收了外框的点击事件，不做处理
        if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
            return true;
        } else {
            return super.onTouchEvent(event);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
